import java.util.concurrent.CountDownLatch;

/**
 * @author JeanHu
 * @date 2025/11/5
 * @description
 */
public class test2 {
    public static void main(String[] args) {
        int n = 10;
        CountDownLatch[] latches = new CountDownLatch[n];

        for (int i = 0; i < n; i++) {
            latches[i] = new CountDownLatch(1);
        }

        for (int i = 0; i < n; i++) {
            final int index = i;
            new Thread(()->{
                try {
                    if(index > 0){
                        latches[index-1].await();
                    }
                    System.out.println("线程"+ (index+1)+"打印"+(index+1));
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally {
                    latches[index].countDown();
                }

            }).start();
        }
    }
}
